/**
 * 简单的文档路由
 * 替代Swagger插件，提供基础的API文档
 */

async function docsRoutes(fastify, options) {
  // 基础文档页面
  fastify.get('/docs', async (request, reply) => {
    const html = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multi-Tenant SaaS API 文档</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 { color: #2c3e50; border-bottom: 3px solid #3498db; padding-bottom: 10px; }
        h2 { color: #34495e; margin-top: 30px; }
        .endpoint {
            background: #f8f9fa;
            border-left: 4px solid #007bff;
            padding: 15px;
            margin: 10px 0;
            border-radius: 4px;
        }
        .method {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-weight: bold;
            margin-right: 10px;
        }
        .get { background: #28a745; color: white; }
        .post { background: #007bff; color: white; }
        .put { background: #ffc107; color: black; }
        .delete { background: #dc3545; color: white; }
        .description { margin-top: 10px; color: #666; }
        .auth-required { color: #e74c3c; font-weight: bold; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 Multi-Tenant SaaS API 文档</h1>
        <p><strong>版本:</strong> 1.0.0</p>
        <p><strong>基础URL:</strong> http://localhost:3000/api/v1</p>
        
        <h2>📋 系统端点</h2>
        <div class="endpoint">
            <span class="method get">GET</span><code>/api/v1/info</code>
            <div class="description">获取API基本信息</div>
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span><code>/api/v1/test/health</code>
            <div class="description">健康检查端点</div>
        </div>
        
        <h2>🔐 认证端点</h2>
        <div class="endpoint">
            <span class="method post">POST</span><code>/api/v1/auth/register</code>
            <div class="description">用户注册 <span class="auth-required">(需要租户ID)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span><code>/api/v1/auth/login</code>
            <div class="description">用户登录 <span class="auth-required">(需要租户ID)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span><code>/api/v1/auth/refresh</code>
            <div class="description">刷新令牌 <span class="auth-required">(需要认证)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span><code>/api/v1/auth/logout</code>
            <div class="description">用户登出 <span class="auth-required">(需要认证)</span></div>
        </div>
        
        <h2>🏢 租户管理端点</h2>
        <div class="endpoint">
            <span class="method get">GET</span><code>/api/v1/tenants</code>
            <div class="description">获取租户列表 <span class="auth-required">(需要系统管理员权限)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span><code>/api/v1/tenants</code>
            <div class="description">创建新租户 <span class="auth-required">(需要系统管理员权限)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span><code>/api/v1/tenants/:id</code>
            <div class="description">获取特定租户信息 <span class="auth-required">(需要认证)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method put">PUT</span><code>/api/v1/tenants/:id</code>
            <div class="description">更新租户信息 <span class="auth-required">(需要租户管理员权限)</span></div>
        </div>
        
        <h2>👥 用户管理端点</h2>
        <div class="endpoint">
            <span class="method get">GET</span><code>/api/v1/users</code>
            <div class="description">获取当前租户用户列表 <span class="auth-required">(需要认证)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span><code>/api/v1/users/profile</code>
            <div class="description">获取当前用户资料 <span class="auth-required">(需要认证)</span></div>
        </div>
        
        <div class="endpoint">
            <span class="method put">PUT</span><code>/api/v1/users/profile</code>
            <div class="description">更新当前用户资料 <span class="auth-required">(需要认证)</span></div>
        </div>
        
        <h2>🔑 认证说明</h2>
        <div class="endpoint">
            <p><strong>租户识别:</strong> 通过 <code>X-Tenant-ID</code> 请求头传递</p>
            <p><strong>身份认证:</strong> 通过 <code>Authorization: Bearer &lt;token&gt;</code> 请求头传递</p>
            <p><strong>示例请求:</strong></p>
            <pre style="background: #f1f1f1; padding: 10px; border-radius: 4px;">
curl -X POST http://localhost:3000/api/v1/auth/register \\
  -H "Content-Type: application/json" \\
  -H "X-Tenant-ID: your-tenant-id" \\
  -d '{"email": "user@example.com", "password": "password123", "name": "User Name"}'
            </pre>
        </div>
        
        <h2>📊 状态码</h2>
        <div class="endpoint">
            <p><strong>200:</strong> 请求成功</p>
            <p><strong>201:</strong> 资源创建成功</p>
            <p><strong>400:</strong> 请求参数错误</p>
            <p><strong>401:</strong> 未认证</p>
            <p><strong>403:</strong> 权限不足</p>
            <p><strong>404:</strong> 资源未找到</p>
            <p><strong>500:</strong> 服务器内部错误</p>
        </div>
    </div>
</body>
</html>
    `;
    
    reply.type('text/html').send(html);
  });

  // API信息端点
  fastify.get('/docs/api', async (request, reply) => {
    return {
      name: 'Multi-Tenant SaaS API',
      version: '1.0.0',
      description: 'A scalable multi-tenant SaaS backend built with Fastify',
      baseUrl: `http://localhost:${process.env.PORT || 3000}/api/v1`,
      endpoints: {
        system: [
          { method: 'GET', path: '/info', description: '获取API基本信息' },
          { method: 'GET', path: '/test/health', description: '健康检查' }
        ],
        auth: [
          { method: 'POST', path: '/auth/register', description: '用户注册', requiresTenant: true },
          { method: 'POST', path: '/auth/login', description: '用户登录', requiresTenant: true },
          { method: 'POST', path: '/auth/refresh', description: '刷新令牌', requiresAuth: true },
          { method: 'POST', path: '/auth/logout', description: '用户登出', requiresAuth: true }
        ],
        tenants: [
          { method: 'GET', path: '/tenants', description: '获取租户列表', requiresAuth: true, requiresAdmin: true },
          { method: 'POST', path: '/tenants', description: '创建新租户', requiresAuth: true, requiresAdmin: true },
          { method: 'GET', path: '/tenants/:id', description: '获取租户信息', requiresAuth: true },
          { method: 'PUT', path: '/tenants/:id', description: '更新租户信息', requiresAuth: true }
        ],
        users: [
          { method: 'GET', path: '/users', description: '获取用户列表', requiresAuth: true },
          { method: 'GET', path: '/users/profile', description: '获取用户资料', requiresAuth: true },
          { method: 'PUT', path: '/users/profile', description: '更新用户资料', requiresAuth: true }
        ]
      }
    };
  });
}

module.exports = docsRoutes;